//
//  ResultViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/4/1.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "ResultViewController.h"
#import "FeedCell.h"
#import "FeedModel.h"
#import "FollowingTableViewCell.h"
#import "LLZStatusBarAlertView.h"
#import "VisitViewController.h"
#import "XLPhotoBrowser.h"
#import "OneFeedViewController.h"
#import "VisitViewController.h"

@interface ResultViewController ()<UITableViewDelegate, UITableViewDataSource, FeedCellDelegate, XLPhotoBrowserDelegate>

@end

@implementation ResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - *******tableview DataSource*******
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(_type == 0){///搜索用户的结果
        FollowingTableViewCell *followCell = [tableView cellForRowAtIndexPath:indexPath];
        if (!followCell) {
            followCell = [[[NSBundle mainBundle]loadNibNamed:@"FollowingTableViewCell" owner:self options:nil]firstObject];
        }
        followCell.model = self.testData[indexPath.section];
        return followCell;//返回用户cell
    }
    ///搜索标签、内容的结果
    FeedCell *feedCell = [tableView cellForRowAtIndexPath:indexPath];
    if (!feedCell) {
        feedCell = [[[NSBundle mainBundle]loadNibNamed:@"FeedCell" owner:self options:nil]firstObject];
    }
    [self configureCell:feedCell atIndexPath:indexPath];
    return feedCell;//返回个人信息cell
}

- (void)configureCell:(FeedCell *)cell atIndexPath:(NSIndexPath *)indexPath {
    cell.delegate = self;
    //数据刷新
    FeedModel *model = self.testData[indexPath.section];
    cell.model = model;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.testData.count;
}

#pragma mark - *******tableview Delegate*******

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView cellForRowAtIndexPath:indexPath].selected = NO;
    if (_type == 0) {
        VisitViewController *visit = [[VisitViewController alloc]init];
        [self.navigationController pushViewController:visit animated:YES];
        return;
    }
    OneFeedViewController *one = [[OneFeedViewController alloc]init];
    one.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:one animated:YES];
}

#pragma mark - *******feedDelegate*******
///关注按钮被点击
- (void)followBtnClicked:(FeedCell *)feedCell button:(UIButton *)followBtn{
    if([followBtn.titleLabel.text isEqualToString:@"关注"]){
        [followBtn setTitle:@"取消关注" forState:UIControlStateNormal];
    }else{
        [followBtn setTitle:@"关注" forState:UIControlStateNormal];
    }
}

///价值按钮被点击
- (void)moneyBtnClicked:(FeedCell *)feedCell view:(UIView *)view{
    [SVProgressHUD showInfoWithStatus:@"预计一周后的收入"];
    [SVProgressHUD dismissWithDelay:0.5];
}

///点赞按钮被点击
- (void)thumbBtnClicked:(FeedCell *)feedCell view:(UIView *)view{
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[UIImageView class]]) {
            UIImageView *imageView = (UIImageView *)subview;
            if ([imageView.image isEqual:[UIImage imageNamed:@"good"]]) {
                imageView.image = [UIImage imageNamed:@"good-filling"];
            }else if ([imageView.image isEqual:[UIImage imageNamed:@"good-filling"]]) {
                [SVProgressHUD showErrorWithStatus:@"请勿重复点赞"];
                [SVProgressHUD dismissWithDelay:0.5];
                break;
            }
        }
        if ([subview isKindOfClass:[UILabel class]]) {
            UILabel *label = (UILabel *)subview;
            [label setText:[NSString stringWithFormat:@"%ld",(label.text.integerValue + 1)]];
        }
        
    }
}

///评论按钮被点击
- (void)commentBtnClicked:(FeedCell *)feedCell view:(UIView *)view{
    OneFeedViewController *one = [[OneFeedViewController alloc]init];
    one.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:one animated:YES];
}

///收藏按钮被点击
- (void)collectionBtnClicked:(FeedCell *)feedCell button:(UIButton *)collcetionBtn{
    collcetionBtn.selected = collcetionBtn.selected ? NO : YES;
    [collcetionBtn setImage:[UIImage imageNamed:@"favorites-filling.png"] forState:UIControlStateSelected];
    if (collcetionBtn.selected) {
        [SVProgressHUD showSuccessWithStatus:@"成功收藏"];
    }else{
        [SVProgressHUD showSuccessWithStatus:@"取消收藏"];
    }
    [SVProgressHUD dismissWithDelay:0.5];
}

///头像被点击
- (void)avatorClicked:(NSString *)accountID{
    VisitViewController *visit = [[VisitViewController alloc]init];
    visit.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:visit animated:YES];
}

///预览图片
- (void)photosClicked:(NSArray *)photos{
    NSMutableArray <UIImage *>* images = [[NSMutableArray alloc]init];
    for (NSString *name in photos){
        UIImage *image = [UIImage imageNamed:name];
        [images addObject:image];
    }
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithImages:images currentImageIndex:0];
    [browser setActionSheetWithTitle:nil delegate:self cancelButtonTitle:@"取消" deleteButtonTitle:nil otherButtonTitles:@"保存图片",nil];
    
}

#pragma mark - *******XLPhotoBrowserDelegate*******
- (void)photoBrowser:(XLPhotoBrowser *)browser clickActionSheetIndex:(NSInteger)actionSheetindex currentImageIndex:(NSInteger)currentImageIndex{
    
    switch (actionSheetindex) {
        case 0: {[browser saveCurrentShowImage];}// 保存
            break;
        default:
            break;
    }
}

#pragma mark - *******布局*******
- (void)viewDidLayoutSubviews{
    self.tableview.frame = self.view.bounds;
}

#pragma mark - *******懒加载*******
- (NSMutableArray *)testData{
    if (!_testData) {
        _testData = [[NSMutableArray alloc]init];
    }
    return _testData;
}

- (UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.llz_width, self.view.llz_height) style:UITableViewStyleGrouped];
        _tableview.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, CGFLOAT_MIN)];
        _tableview.dataSource = self;
        _tableview.delegate = self;
        _tableview.estimatedRowHeight = 200;
        _tableview.estimatedSectionFooterHeight = 0;
        _tableview.backgroundColor = BACKGROUND_COLOR;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

@end
